﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class SupplierDAO
    {
        DataServices m_SupplierDAO = new DataServices();

        public DataTable GetAll()
        {
            m_SupplierDAO.Load("GetAllSupplier", null);
            return m_SupplierDAO;
        }

        public DataTable GetById(String supplierId)
        {
            SqlParameter param = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param.Value = supplierId;

            m_SupplierDAO.Load("GetSupplierById", param);
            return m_SupplierDAO;
        }

        public DataTable GetByLikeId(String supplierId)
        {
            SqlParameter param = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param.Value = supplierId;

            m_SupplierDAO.Load("GetSupplierByLikeId", param);
            return m_SupplierDAO;
        }

        public DataTable GetByName(String supplierName)
        {
            SqlParameter param = new SqlParameter("@SupplierName", SqlDbType.NVarChar, 200);
            param.Value = supplierName;

            m_SupplierDAO.Load("GetSupplierByName", param);
            return m_SupplierDAO;
        }

        public DataTable GetByTaxCode(String taxCode)
        {
            SqlParameter param = new SqlParameter("@TaxCode", SqlDbType.VarChar, 50);
            param.Value = taxCode;

            m_SupplierDAO.Load("GetSupplierByTaxCode", param);
            return m_SupplierDAO;
        }

        public DataTable GetByAddress(String address)
        {
            SqlParameter param = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param.Value = address;

            m_SupplierDAO.Load("GetSupplierByAddress", param);
            return m_SupplierDAO;
        }

        public DataTable GetByPhone(String phone)
        {
            SqlParameter param = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param.Value = phone;

            m_SupplierDAO.Load("GetSupplierByPhone", param);
            return m_SupplierDAO;
        }

        public DataTable GetByEmail(String email)
        {
            SqlParameter param = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param.Value = email;

            m_SupplierDAO.Load("GetSupplierByEmail", param);
            return m_SupplierDAO;
        }

        public DataTable GetByWebsite(String website)
        {
            SqlParameter param = new SqlParameter("@Website", SqlDbType.NVarChar, 200);
            param.Value = website;

            m_SupplierDAO.Load("GetSupplierByWebsite", param);
            return m_SupplierDAO;
        }

        public int Add(SupplierInfo info)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param[0].Value = info.SupplierId;

            param[1] = new SqlParameter("@SupplierName", SqlDbType.NVarChar, 200);
            param[1].Value = info.SupplierName;

            param[2] = new SqlParameter("@TaxCode", SqlDbType.VarChar, 50);
            param[2].Value = info.TaxCode;

            param[3] = new SqlParameter("@State", SqlDbType.NVarChar, 100);
            param[3].Value = info.State;

            param[4] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[4].Value = info.Address;

            param[5] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[5].Value = info.Phone;

            param[6] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[6].Value = info.Email;

            param[7] = new SqlParameter("@Website", SqlDbType.NVarChar, 200);
            param[7].Value = info.Website;

            param[8] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[8].Value = info.Remark;

            return m_SupplierDAO.ExecuteUpdate("AddSupplier", param);
        }

        public int Update(SupplierInfo info)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param[0].Value = info.SupplierId;

            param[1] = new SqlParameter("@SupplierName", SqlDbType.NVarChar, 200);
            param[1].Value = info.SupplierName;

            param[2] = new SqlParameter("@TaxCode", SqlDbType.VarChar, 50);
            param[2].Value = info.TaxCode;

            param[3] = new SqlParameter("@State", SqlDbType.NVarChar, 100);
            param[3].Value = info.State;

            param[4] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[4].Value = info.Address;

            param[5] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[5].Value = info.Phone;

            param[6] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[6].Value = info.Email;

            param[7] = new SqlParameter("@Website", SqlDbType.NVarChar, 200);
            param[7].Value = info.Website;

            param[8] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[8].Value = info.Remark;

            return m_SupplierDAO.ExecuteUpdate("UpdateSupplier", param);
        }

        public int Delete(String supplierId)
        {
            SqlParameter param = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param.Value = supplierId;

            return m_SupplierDAO.ExecuteUpdate("DeleteSupplier", param);
        }

        public DataRow AddNewRow()
        {
            return m_SupplierDAO.NewRow();
        }

        public void AddNewSupplier(DataRow m_Row)
        {
            m_SupplierDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_SupplierDAO.ExecuteNoneQuery() > 0;
        }
    }
}
